package com.wf.it.common.util;

import com.sun.org.apache.xpath.internal.objects.XObject;
import com.wf.it.common.entity.Dept;
import com.wf.it.common.entity.Doctor;
import com.wf.it.common.vo.Result;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

public class ForeignHelper {

    public static Result fillMap(
            Result pkResult,Object fkProxy,String fkProxyMethod
            ,String pkKeyProperyName,String pkMapProperyName,String resultMapKey){
        try {
            Method[] methods=fkProxy.getClass().getDeclaredMethods();
            Method fkMethod=null;
            for(Method m:methods){
                if(m.getName().equalsIgnoreCase(fkProxyMethod)) {
                    fkMethod = m;
                    break;
                }
            }
            Class argClass=fkMethod.getParameterTypes()[0];
            Object arg=null;
            if(argClass.isArray()) {
                Set set = getKeys(pkResult, pkKeyProperyName);
                arg = Array.newInstance(argClass.getComponentType(), set.size());
                set.toArray((Object[]) arg);
            }else{
                Object pk=pkResult.getObj();
                Field keyField= pk.getClass().getDeclaredField(pkKeyProperyName);
                keyField.setAccessible(true);
                arg=keyField.get(pk);
            }
            Result fkResult = (Result) fkMethod.invoke(fkProxy, new Object[]{arg});
            fillMap(pkResult,fkResult,pkKeyProperyName,pkMapProperyName,resultMapKey);
            return pkResult;
        }catch (Exception ex){
            throw new RuntimeException(ex);
        }
    }

    public static void fillMap(Result pkResult,Result fkResult
            ,String pkKeyProperyName,String pkMapProperyName,String resultMapKey){
        try {

            List pkList=null;
            if(pkResult.getObj() instanceof Collection)
               pkList=(List) pkResult.getObj();
            else
               pkList=Collections.singletonList(pkResult.getObj());
            for(Object pk:pkList) {
                //获取doc.getDeptName()
                Field keyField= pk.getClass().getDeclaredField(pkKeyProperyName);
                keyField.setAccessible(true);
                Object fkvalue=keyField.get(pk);
                //doc.getMap();
                Field mapField= pk.getClass().getDeclaredField(pkMapProperyName);
                mapField.setAccessible(true);
                Map<String,Object> pkmap= (Map<String, Object>) mapField.get(pk);

                if(fkResult.getState()==200) {
                    Object fkValue=fkResult.getObj();
                    if(fkValue instanceof Map)
                        pkmap.put(resultMapKey, ((Map)fkValue).get(fkvalue));
                    else
                        pkmap.put(resultMapKey,fkValue);
                }
                else
                    pkmap.put(resultMapKey+"_err",fkResult.getMsg());
            }
        }catch (Exception ex){
            throw new RuntimeException(ex);
        }
    }

    public  static <T> Set<T> getKeys(Result srcRes,String keyProperyName){
        return ForeignHelper.<T>getKeys((List)srcRes.getObj(),keyProperyName);
    }
    public  static <T> Set<T> getKeys(List srcList,String keyProperyName){
        try {
            Set<T> set = new HashSet();
            Class c = null;
            for (Object o : srcList) {
                if (c == null)
                    c = o.getClass();
                Field keyField = c.getDeclaredField(keyProperyName);
                keyField.setAccessible(true);
                T value= (T) keyField.get(o);
                set.add(value);
            }
            return set;
        }catch (Exception ex){
            throw new RuntimeException(ex);
        }
    }


    //-------------------------------------------------------------------------
    public  static String[] getStringKeys(Result srcRes,String keyProperyName){
        Set<String> keys= ForeignHelper.<String>getKeys(srcRes,keyProperyName);
        return keys.toArray(new String[keys.size()]);
    }

    public  static Integer[] getIntKeys(Result srcRes,String keyProperyName){
        Set<Integer> keys= ForeignHelper.<Integer>getKeys(srcRes,keyProperyName);
        return keys.toArray(new Integer[keys.size()]);
    }

    public  static Long[] getLongKeys(Result srcRes,String keyProperyName){
        Set<Long> keys= ForeignHelper.<Long>getKeys(srcRes,keyProperyName);
        return keys.toArray(new Long[keys.size()]);
    }
    //----------------------------------------------------------------------------
    public  static String[] getStringKeys(List srcList,String keyProperyName){
        Set<String> keys= ForeignHelper.<String>getKeys(srcList,keyProperyName);
        return keys.toArray(new String[keys.size()]);
    }

    public  static Integer[] getIntKeys(List srcList,String keyProperyName){
        Set<Integer> keys= ForeignHelper.<Integer>getKeys(srcList,keyProperyName);
        return keys.toArray(new Integer[keys.size()]);
    }

    public  static Long[] getLongKeys(List srcList,String keyProperyName){
        Set<Long> keys= ForeignHelper.<Long>getKeys(srcList,keyProperyName);
        return keys.toArray(new Long[keys.size()]);
    }


}
